﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace UFrame
{
    /// <summary>
    /// 基础的UI管理
    /// </summary>
    public static class UIBasicsMa
    {
        public static GameObject mUIRoot;
        private static Dictionary<string, BaseBasicsUI> mUIDic = new Dictionary<string, BaseBasicsUI>();

        public static string path;

        public static T GetUI<T>(string uiName) where T : BaseBasicsUI
        {
            if (mUIDic.ContainsKey(uiName))
            {
                return mUIDic[uiName] as T;
            }
#if UNITY_EDITOR
            Debug.LogError("没有找到uiName " + uiName);
#endif
            return null;
        }

        public static T GetUI<T>() where T : BaseBasicsUI
        {
            BaseBasicsUI uiRef = null;
            foreach (var item in mUIDic)
            {
                Type ui = item.Value.GetType();
                if (ui == typeof(T))
                {
                    uiRef = item.Value;
                    break;
                }
            }

            return uiRef as T;
        }

        public static BaseBasicsUI ShowUI(string uiName, object arg = null)
        {
            return ShowUI<BaseBasicsUI>(uiName, arg);
        }

        public static T ShowUI<T>(string uiName, object arg = null) where T : BaseBasicsUI
        {
            string d = path + uiName;

            BaseBasicsUI uiScript = null;
            if (mUIDic.ContainsKey(uiName))
            {
                uiScript = mUIDic[uiName];
                if (uiScript.gameObject.activeInHierarchy == false)
                {
                    uiScript.gameObject.SetActive(true);
                }
                uiScript.OnUIRefresh(arg);
            }
            else
            {
                var uiNodePre = Resources.Load<GameObject>(d);
                if (uiNodePre == null)
                {
                    Debug.LogError("没有找到UI: " + d);
                }

                var uiNode = GameObject.Instantiate(uiNodePre);
                uiNode.transform.SetParent(mUIRoot.transform, false);

                uiScript = uiNode.GetComponent<BaseBasicsUI>();
                mUIDic.Add(uiName, uiScript);
                uiNode.gameObject.SetActive(true);
                uiScript.uiName = uiName;
                uiScript.OnInit(arg);
                uiScript.OnUIRefresh(arg);
            }

            return uiScript as T;
        }

        public static T ShowUI<T>(object arg = null) where T : BaseBasicsUI
        {
            string uiName = typeof(T).Name;
            return ShowUI<T>(uiName, arg);
        }

        public static T CloseUI<T>(string uiName) where T : BaseBasicsUI
        {
            BaseBasicsUI uiScript = null;
            if (mUIDic.ContainsKey(uiName))
            {
                uiScript = mUIDic[uiName];
                uiScript.gameObject.SetActive(false);
                uiScript.OnUIClose();
            }

            return uiScript as T;
        }

        public static BaseBasicsUI CloseUI(string uiName)
        {
            BaseBasicsUI uiScript = null;
            if (mUIDic.ContainsKey(uiName))
            {
                uiScript = mUIDic[uiName];
                uiScript.gameObject.SetActive(false);
                uiScript.OnUIClose();
            }

            return uiScript;
        }


        public static T CloseUI<T>() where T : BaseBasicsUI
        {
            BaseBasicsUI uiRef = null;
            foreach (var item in mUIDic)
            {
                Type ui = item.Value.GetType();
                if (ui == typeof(T))
                {
                    uiRef = item.Value;
                    break;
                }
            }

            if (uiRef != null)
                uiRef.gameObject.SetActive(false);

            return uiRef as T;
        }


        public static T DestroyUI<T>(string uiName) where T : BaseBasicsUI
        {
            BaseBasicsUI uiScript = null;
            if (mUIDic.ContainsKey(uiName))
            {
                uiScript = mUIDic[uiName];
                uiScript.gameObject.SetActive(false);
                uiScript.OnUIClose();
                mUIDic.Remove(uiName);
                GameObject.Destroy(uiScript.gameObject);
            }


            return uiScript as T;
        }
    }
}